/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pneuservis.backend.to;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import pneuservis.backend.entities.Customer;
import pneuservis.backend.entities.User;

/**
 *
 * @author Admin
 */
public class CustomerTO {
    private Long id;
    private String name;
    private String phone;
    private String address;
    private String username;
    private String password;
    private List<UserTO> roles  = new ArrayList<>();

    public CustomerTO() {
    }

    public CustomerTO(Customer customer) {
        this.id = customer.getId();
        this.name = customer.getName();
        this.phone = customer.getTelephone();
        this.address = customer.getAddress();
        this.username = customer.getUsername();
        this.password = customer.getPassword();
        for(User user : customer.getRoles()) {
            UserTO userTO = new UserTO();
            userTO.setId(user.getId());
            userTO.setUser(this);
            userTO.setPermission(user.getPermission());
            roles.add(userTO);
        }
        this.setRoles(roles);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String telePhone) {
        this.phone = telePhone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<UserTO> getRoles() {
         if (this.roles == null) return null;
        return Collections.unmodifiableList(this.roles);
    }

    public void setRoles(List<UserTO> roles) {
        this.roles = null;

        if (roles != null) {
            this.roles = new ArrayList<UserTO>();
            this.roles.addAll(roles);
        }
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 83 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final CustomerTO other = (CustomerTO) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "CustomerTO{" + "id=" + id + ", name=" + name + '}';
    }
    
    public Customer toCustomer() {
        Customer customer = new Customer();
        customer.setId(id);
        customer.setName(name);
        customer.setTelephone(phone);
        customer.setAddress(address);
        customer.setUsername(username);
        customer.setPassword(password);
        List<User> roles = new ArrayList<>();
        for (UserTO userTO : getRoles()) {
            User user = new User();
            user.setId(userTO.getId());
            user.setPermission(userTO.getPermission());
            user.setUser(customer);
            roles.add(user);
        }
        customer.setRoles(roles);
        return customer;
    }
}
